//
//  WPRewardsViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/3/31.
//  Copyright © 2016年 HuaYiSoftware. All rights reserved.
//

#import "WPRewardsViewController.h"
#import "WPNewsPreheatModel.h"
#import "WPNewsPreheatTableViewCell.h"
#import "WPNewsDealingModel.h"
#import "WPNewsDealingTableViewCell.h"
#import "WPTradeListViewController.h"

static NSString *const kUITableViewCellIdentifier = @"cellIdentifier";

@interface WPRewardsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UISegmentedControl * segmentControl;/**< 分段控件 */
@property (nonatomic, strong) UITableView * tradeTableView;/**< 交易表格视图 */
@property (nonatomic, strong) NSMutableArray * tradeArr;/**< 交易数据源 */
@property (nonatomic, strong) UIView * footView;/**< 列表表尾 */


- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */
- (void)refreshData;/**< 刷新数据 */


@end

@implementation WPRewardsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}
- (void)viewWillDisappear:(BOOL)animated {
    [self.segmentControl removeFromSuperview];
}
#pragma mark - init
- (void)initializeDataSource {
    self.tradeArr = [NSMutableArray arrayWithCapacity:0];
    WPNewsPreheatModel * model = [WPNewsPreheatModel new];
    model.posterImageName = @"banner1.png";
    model.title = @"The squall Rain亚洲演唱会";
    model.detail = @"违法龙卷风甲方垃圾分类加了福建福利及发洛杉矶大连三姐夫龙卷风龙卷风垃圾按缴费卡九分裤积分卡了解封了案件发空间分类";
    model.attentionCount = @"40000";
    model.leftDays = @"12";

    WPNewsDealingModel * model1 = [WPNewsDealingModel new];
    model1.posterImageName = @"banner1.png";
    model1.title = @"The squall Rain亚洲演唱会";
    model1.detail = @"违法龙卷风甲方垃圾分类加了福建福利及发洛杉矶大连三姐夫龙卷风龙卷风垃圾按缴费卡九分裤积分卡了解封了案件发空间分类";
    model1.ticketCount = @"750";
    model1.allTicketCount = @"800";
    model1.joinCount = @"200";
    model1.leftDays = @"8";
    for (int i = 0; i < 2; i++) {
        [_tradeArr addObject:model];
    }
    [_tradeArr addObject:model1];
}
- (void)initializeUserInterface {
    [self.navigationController.navigationBar addSubview:self.segmentControl];
    [self.view addSubview:self.tradeTableView];
}
#pragma mark - 刷新数据
- (void)refreshData {
    
}
#pragma mark - UITextField Protocol

#pragma mark - 协议UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _tradeArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section != _tradeArr.count - 1) {
        WPNewsPreheatTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell) {
            cell = [[WPNewsPreheatTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"123"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        [cell setModelWithModel:_tradeArr[indexPath.section]];
        return cell;
    }
    WPNewsDealingTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[WPNewsDealingTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"1234"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    [cell setModelWithModel:_tradeArr[indexPath.section]];
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
//设置每一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 280*DHFlexibleHorizontalMutiplier();
}
//用户点击了某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == _tradeArr.count - 1) {
        WPTradeListViewController * vc = [WPTradeListViewController new];
        vc.model = _tradeArr[indexPath.section];
        [self pushToNextController:vc withVCTitle:@"交易期"];

    }
}
#pragma mark -getter
- (UISegmentedControl *)segmentControl {
    if (!_segmentControl) {
        _segmentControl = ({
            UISegmentedControl * seg = [[UISegmentedControl alloc]initWithItems:@[@"票务", @"产品"]];
            seg.bounds = CGRectMake(0, 0, 130*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            seg.center = CGPointMake(self.view.center.x, navBar_height/2.0);
            seg.selectedSegmentIndex = 0;
            seg;
        });
    }
    return _segmentControl;
}
- (UIView *)footView {
    if (!_footView) {
        _footView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 70*DHFlexibleHorizontalMutiplier())];
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, 30*DHFlexibleHorizontalMutiplier())];
            lab.textAlignment = NSTextAlignmentCenter;
            lab.textColor = COLOR_RGB(141, 141, 141, 1);
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.text = @"没有更多数据";
            [view addSubview:lab];
            view;
        });
    }
    return _footView;
}
- (UITableView *)tradeTableView {
    if (!_tradeTableView) {
        _tradeTableView = ({
            UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, navBar_Fheight + 5*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, SCREEN_SIZE.height - navBar_Fheight - 5*DHFlexibleHorizontalMutiplier()) style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            tableview.tableFooterView = self.footView;
            tableview.pagingEnabled = NO;
            tableview.separatorColor = [UIColor clearColor];
            tableview;
        });
    }
    return _tradeTableView;
}

@end
